package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lixu
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    /**
     * select name from dts_role where id in (1,2,3,4)
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> findRolesByRoleIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(DtsRole::getName)
                .in(DtsRole::getId,roleIds)
                .eq(DtsRole::getEnabled,true)//启用
                .eq(DtsRole::getDeleted,false);//不删除
        List<DtsRole> dtsRoleList = dtsRoleMapper.selectList(lambdaQueryWrapper);
        //偷天换日
        return dtsRoleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
    }

    @Override
    public List<DtsRole> findRoleList() {
        return dtsRoleMapper.selectList(null);
    }

    /**
     * 分页查询
     * @param rolename
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<DtsRole> findRolePageByQuery(String rolename, String sort, String order, Integer page, Integer limit) {
        return dtsRoleMapper.selectPage(new Page<DtsRole>(page,limit)
        ,new QueryWrapper<DtsRole>()
                        .eq(!StringUtils.isEmpty(rolename),"name",rolename)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)
        );
    }


}
